home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Games / Game Sample Code / ZAM 1.0a13 / GameSource / xthing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  4.0 KB  |  188 lines  |  [TEXT/KAHL]

  1. /*
  2.     xthing
  3.     ------
  4.     A little set of routines that allow you to stuff
  5.     tasks in the time manager queue, which will be fed back to you
  6.     in the XThingList queue, accessed within the application's
  7.     main loop by calling ProcessXThingTask.
  8.         
  9.     7:17:22 PM  10/28/92
  10.     By Brigham Stevens
  11.     Apple Computer, Inc.
  12.     Developer Technical Support
  13.     
  14.     
  15.     At init time call InitXThingTimer.
  16.     Call StartXThing to add a task and start its clock.
  17.     Call AddXThing to only add a task and NOT start its clock.
  18.     At the end of your program call KillAllXThingTasks
  19.     If your task wants to remove itself, it should return false.
  20.     
  21.     Xthing tasks have the form of:
  22.     
  23.     Boolean XThingUpdateTask(xthing *xtp, long dataRef)
  24.     {
  25.         myDataStruct *mdp;
  26.         Boolean         result;
  27.         
  28.         mdp = (myDataStruct *)dataRef;
  29.         
  30.         mdp.count++;                // do something usefull (less)
  31.         
  32.         if(mdp.count)
  33.             result = true;            // want to be run again
  34.         else
  35.             result = false;            // tell Xthing not to re-install us
  36.     }
  37.  
  38.     See Sprite.c for more examples.
  39.     
  40. */
  41.  
  42. #include "xthing.h"
  43. #include "ZAMProtos.h"
  44.  
  45. static void XThingTimeTask(void);
  46.  
  47. static long        xThingCount = 0;
  48. static xQHdr    XThingList;
  49.  
  50. void InitXThingTimer(void)
  51. {
  52.     xInitQueueHeader(&XThingList);
  53. }
  54.  
  55. static void XThingTimeTask(void)
  56. {    
  57.     /* Time manager points to time task record in a1 */
  58.     /* We have all our data located after that, so it */
  59.     /* is simple to just do our  thing */
  60.     
  61.     asm {    
  62.             st.b        xthing.taskFlag(a1)            
  63.     }
  64. }
  65.  
  66. void ProcessXThingTask(short numTasksToProcess)
  67. /*
  68.     Whip through the queue until there
  69.     are no more entires.  Each item is
  70.     ripped from the queue, it's associated task
  71.     is called, and the item is then fed back to
  72.     the Time Manager task, which will give it back to us here
  73.     when it is done chewing on it.
  74.     
  75.     If the user interface slows down because of too many things going on,
  76.     you might want to add a task that basically calls your event loop
  77.     once in a while.  Then you could make this the main loop of your
  78.     program if you were so inclined.
  79. */
  80. {
  81.     xthing            *xtp;
  82.     updateProc        updateJSR;
  83.     Boolean            reTime;
  84.     OSErr            err;
  85.     
  86.     for(xtp = (xthing*)XThingList.qHead; xtp != nil; xtp = xtp->next) {
  87.         if(xtp->taskFlag) {
  88.             xtp->taskFlag = false;
  89.             updateJSR = (updateProc)xtp->actionProc;
  90.             if(updateJSR)  {
  91.                 reTime = (*updateJSR)(xtp,xtp->dataRef);
  92.                 if(reTime) {
  93.                     RmvTime(&xtp->timer);
  94.                     InsTime(&xtp->timer);
  95.                     PrimeTime(&xtp->timer, xtp->interval);
  96.                 }
  97.             }
  98.         }
  99.     }
  100. }
  101.  
  102.  
  103. void EnqueueXThing(xthing *xtp)
  104. {
  105.  
  106.     xThingCount++;
  107.     
  108.     if(xtp->inList == false) {            // change to bit field in taskFlag later
  109.         xEnqueue(xtp,&XThingList);
  110.         xtp->inList = true;
  111.     }
  112. }
  113.  
  114. void AddXThing(xthing *xtp, long prime, updateProc updtProc, long dataRef)
  115. /*
  116.     This adds a thing to the xthing list, but does not start the tasks
  117. */
  118. {
  119.     short    err = noErr;
  120.         
  121.     if(err == noErr) {
  122.         /* set up default values */
  123.         
  124.         xtp->timer.tmAddr = (ProcPtr)XThingTimeTask;
  125.         
  126.         xtp->interval = prime;
  127.         xtp->taskFlag = false;    
  128.         xtp->dataRef = dataRef;
  129.         xtp->actionProc = updtProc;
  130.         xtp->waiting = true;
  131.  
  132.         EnqueueXThing(xtp);
  133.     }
  134.  
  135. }
  136.  
  137.  
  138. xthing *StartXThing(xthing *xtp, long prime, updateProc updtProc, long dataRef)
  139. /*
  140.     The best thing about this
  141.     is that it takes almost NO parameters
  142.     pass the timing interval in prime
  143.     a pointer to the update routine in updtProc
  144.     and an application-defined reference in refcon.
  145. */
  146. {
  147.     short    err = noErr;
  148.     
  149.     /* allocate a new thing */
  150.     if(xtp == nil) {
  151.         xtp = (xthing*)NewPtrClear(sizeof(xthing));
  152.         if(!xtp){
  153.             err = MemError();
  154.             ErrMsgCode("\pNewPtrClear failed in StartThing",err);
  155.         }
  156.     }
  157.     
  158.     if(err == noErr) {
  159.         /* set up default values */
  160.         
  161.         xtp->timer.tmAddr = (ProcPtr)XThingTimeTask;
  162.         
  163.         xtp->interval = prime;
  164.         xtp->taskFlag = false;    
  165.         xtp->dataRef = dataRef;
  166.         xtp->actionProc = updtProc;
  167.         xtp->waiting = true;
  168.  
  169.         EnqueueXThing(xtp);
  170.         
  171.         InsTime((QElemPtr)&xtp->timer);
  172.         PrimeTime(&xtp->timer, xtp->interval);
  173.     }
  174.  
  175.     return xtp;
  176. }
  177.  
  178.  
  179. void KillAllXThingTasks(void)
  180. {
  181.     xthing            *xtp;
  182.     
  183.     for(xtp = (xthing*)XThingList.qHead; xtp != nil; xtp = xtp->next) {
  184.         if( (xtp->timer.qType & TaskActiveFlag) != 0)
  185.             RmvTime(&xtp->timer);
  186.     }
  187. }
  188.